package com.museum.guide.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.museum.guide.entity.CollectionCategory;
import com.museum.guide.mapper.CollectionCategoryMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CollectionCategoryService extends ServiceImpl<CollectionCategoryMapper, CollectionCategory> {

    /**
     * 分页查询分类信息
     */
    public Page<CollectionCategory> page(Page<CollectionCategory> page, String name) {
        LambdaQueryWrapper<CollectionCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasText(name), CollectionCategory::getName, name)
                .orderByAsc(CollectionCategory::getSortOrder);
        return page(page, wrapper);
    }

    /**
     * 获取所有分类列表
     */
    public List<CollectionCategory> list(String name) {
        LambdaQueryWrapper<CollectionCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasText(name), CollectionCategory::getName, name)
                .orderByAsc(CollectionCategory::getSortOrder);
        return list(wrapper);
    }

    /**
     * 新增分类
     */
    public void saveCategory(CollectionCategory category) {
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        save(category);
    }

    /**
     * 更新分类信息
     */
    public void updateCategory(CollectionCategory category) {
        category.setUpdateTime(LocalDateTime.now());
        updateById(category);
    }

    /**
     * 删除分类
     */
    public void removeCategory(Long id) {
        removeById(id);
    }
}